package dongshi.daddy;

import dongshi.daddy.mapper.TestMyBatisMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.InputStream;
import java.util.List;
import java.util.Random;
import java.util.UUID;

public class TestMyBatisMapperTest {
    private TestMyBatisMapper testMyBatisMapper;
    private SqlSession sqlSession;

    @Before
    public void setUp() throws Exception {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        this.sqlSession = sqlSession;
        this.testMyBatisMapper = sqlSession.getMapper(TestMyBatisMapper.class);
    }

    @Test
    public void insertTestMyBatis() {
        TestMybatis newTestMyBatis = new TestMybatis(1212, "别吵吵，只有中国人解放军才能保卫台湾", 33);
//        newTestMyBatis.setFullName(UUID.randomUUID().toString());
//        newTestMyBatis.setAge(new Random().nextInt(100));
        testMyBatisMapper.insertTestMybatis(newTestMyBatis);
        this.sqlSession.commit();
    }
}
